package com.zego.wawaji.ui.center.address;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.widget.TextView;

import com.zego.wawaji.R;
import com.zego.wawaji.base.module.ModuleMgr;
import com.noble.library.observer.MsgMgr;
import com.zego.wawaji.base.observer.MsgType;
import com.noble.library.observer.PObserver;
import com.zego.wawaji.base.toast.PToast;
import com.zego.wawaji.http.request.HttpResponse;
import com.zego.wawaji.http.request.RequestComplete;
import com.zego.wawaji.http.request.UrlParam;
import com.zego.wawaji.ui.UIShow;
import com.zego.wawaji.ui.base.BaseAppCompatActivity;
import com.zego.wawaji.ui.base.CustomFrameLayout;
import com.zego.wawaji.ui.base.CustomRecyclerView;
import com.zego.wawaji.ui.center.address.bean.AddressList;
import com.zego.wawaji.ui.dialog.PickerDialogUtil;

import org.json.JSONObject;

/**
 * 收获地址管理
 * Created by Su on 2017/11/27.
 */
public class MyAddressAct extends BaseAppCompatActivity implements RequestComplete, PObserver {
    private CustomFrameLayout frameLayout;
    private CustomRecyclerView statusLayout;
    private RecyclerView recyclerView;
    private AddressAdapter addressAdapter;
    private TextView confirmSend;

    private String strIds;  // 需发货的娃娃id
    private int channel;  // 来源： 0： 添加收获地址  1： 确认发货  2: 充值排行榜发货
    private boolean isDefault = false;

    private String ids;    // 充值排行榜发货

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_address_act);
        initView();
        reqData();
    }

    private void reqData() {
        ModuleMgr.getCenterMgr().reqGetAddress(this);
    }

    private void initView() {
        setBackView();
        setTitle("管理收货地址");
        channel = getIntent().getIntExtra("channel", 0);
        strIds = getIntent().getStringExtra("strIds");
        ids = getIntent().getStringExtra("ids");
        MsgMgr.getInstance().attach(this);
        frameLayout = (CustomFrameLayout) findViewById(R.id.custom_frame);
        frameLayout.showOfIndex(channel == 2 ? 1 : channel);

        statusLayout = (CustomRecyclerView) findViewById(R.id.statusLayout);
        recyclerView = statusLayout.getRecyclerView();
        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        addressAdapter = new AddressAdapter();
        recyclerView.setAdapter(addressAdapter);

        findViewById(R.id.add_address).setOnClickListener(v -> UIShow.showMyEditAddressAct(MyAddressAct.this, null));

        confirmSend = (TextView) findViewById(R.id.confirm_send);
        confirmSend.setOnClickListener(v -> confirmSend());
    }

    private void confirmSend() {
        confirmSend.setEnabled(false);
        if (channel == 2) {
            ModuleMgr.getWawaMgr().reqAwardSend(ids, this);
        } else {
            ModuleMgr.getWawaMgr().reqSendChooseWawa(strIds, this);
        }
    }

    @Override
    public void onRequestComplete(HttpResponse response) {
        if (response.getUrlParam() == UrlParam.reqGetAddress) {
            if (!response.isOk()) {
                frameLayout.showOfIndex(0);
                statusLayout.showNetError(v -> reqData());
                return;
            }

            AddressList dataList = (AddressList) response.getBaseData();
            if (dataList == null || dataList.getAddressList().size() <= 0) {
                frameLayout.showOfIndex(0);
                statusLayout.showNoData();
                return;
            }

            if (frameLayout != null)
                frameLayout.showOfIndex(channel == 2 ? 1 : channel);
            addressAdapter.setList(dataList.getAddressList());
            statusLayout.showRecyclerView();

            if (dataList.getAddressList().size() == 1) {  // 一条地址数据，设置为默认
                if (!isDefault) { // 还未设置默认
                    isDefault = true;
                    ModuleMgr.getCenterMgr().reqDefaultAddress(dataList.getAddressList().get(0).getId(), null, false);
                }
            }
            return;
        }

        // 全部发货
        if (response.getUrlParam() == UrlParam.reqSendChooseWawa) {
            confirmSend.setEnabled(true);
            if (response.isOk()) {
                JSONObject jsonObject = response.getResponseObject();
                int coin = jsonObject.optInt("WwCoinZs");
                if (coin <= 0) {
                    PToast.showShort("发货请求成功");
                } else {
                    PickerDialogUtil.showSendAwardDialog(this, coin);
                }
                MsgMgr.getInstance().sendMsg(MsgType.MT_Wawa_Untreated, null);
                MsgMgr.getInstance().sendMsg(MsgType.MT_Update_MyInfo, null);
            }
        }

        // 充值排行榜
        if (response.getUrlParam() == UrlParam.reqAwardSend) {
            confirmSend.setEnabled(true);
            if (response.isOk()) {
                MsgMgr.getInstance().sendMsg(MsgType.MT_Prize_Refresh, null);
                this.finish();
            }
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        MsgMgr.getInstance().detach(this);
    }

    @Override
    public void onMessage(String key, Object value) {
        switch (key) {
            case MsgType.MT_Updata_Address:
                reqData();
                break;
        }
    }
}
